﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;

namespace Akiha.Core
{
    public class SDE
    {
        #region AE认证
        private static IAoInitialize _aoInitialize;
        /// <summary>
        /// 初始化AE控件
        /// </summary>
        public static bool InitLicense()
        {
            _aoInitialize = new AoInitializeClass();
            if (_aoInitialize.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB) == esriLicenseStatus.esriLicenseAvailable &&
                _aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB) == esriLicenseStatus.esriLicenseCheckedOut)
            {
                return true;
            }
            else
            {
                throw new Exception("缺少GDB许可，请重新安装ArcEngine!");
            }
        }

        public static void ShutDownLicense()
        {
            _aoInitialize.Shutdown();
        }

        #endregion

        private static IWorkspace _workspace;

        /// <summary>
        /// 工作空间
        /// </summary>
        public static IWorkspace Workspace
        {
            get { return _workspace; }
            set { _workspace = value; }
        }

        /// <summary>
        /// 获取SDE工作空间
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="user">用户</param>
        /// <param name="pass">密码</param>
        /// <param name="version">版本</param>
        /// <param name="port">端口</param>
        /// <returns></returns>
        static public IWorkspace GetWorkSpaceFromSDE(string server, string user, string pass, string version, string port)
        {
            try
            {
                IPropertySet pPropSet = new PropertySetClass();
                pPropSet.SetProperty("server", server);
                pPropSet.SetProperty("instance", port);
                pPropSet.SetProperty("user", user);
                pPropSet.SetProperty("password", pass);
                pPropSet.SetProperty("version", version);
                SdeWorkspaceFactory sdeWkspFact = new SdeWorkspaceFactoryClass();
                return sdeWkspFact.Open(pPropSet, 0);

            }
            catch (Exception ex)
            {
                throw new Exception("连接SDE失败！");
                throw ex;
            }
        }

        /// <summary>
        /// 获取SDE工作空间
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="user">用户</param>
        /// <param name="pass">密码</param>
        /// <returns></returns>
        static public IWorkspace GetWorkSpaceFromSDE(string server, string user, string pass)
        {
            return GetWorkSpaceFromSDE(server, user, pass, "SDE.DEFAULT", "5151");
        }

        /// <summary>
        /// 获取MDB文件工作空间
        /// </summary>
        /// <param name="strMdb"></param>
        /// <returns></returns>
        static public IWorkspace GetWorkSpaceFromMDB(string strMdb)
        {
            try
            {
                IWorkspaceFactory pFact = new AccessWorkspaceFactoryClass();
                return pFact.OpenFromFile(strMdb, 0);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// 打开GDB工作空间
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        static public IWorkspace GetWorkSpaceFromGDB(string fileName)
        {
            try
            {
                IWorkspaceFactory pFact = new FileGDBWorkspaceFactoryClass();
                return pFact.OpenFromFile(fileName, 0);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// 打开SHP工作空间
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        static public IWorkspace GetWorkSpaceFromSHP(string fileName)
        {
            try
            {
                string path = System.IO.Path.GetDirectoryName(fileName);
                IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
                return workspaceFactory.OpenFromFile(path, 0);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// 获取SDE工作空间，通过Oracle直连方式 11G
        /// </summary>
        /// <param name="oraServer"></param>
        /// <param name="user"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static IWorkspace GetWorkspaceOracle11G(string oraServer, string user, string pwd)
        {
            IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass();
            IPropertySet propSet = new PropertySetClass();
            propSet.SetProperty("SERVER", "");
            propSet.SetProperty("INSTANCE", "sde:oracle11g");
            propSet.SetProperty("USER", user);
            propSet.SetProperty("PASSWORD", pwd + "@" + oraServer);
            propSet.SetProperty("VERSION", "SDE.DEFAULT");
            IWorkspace pWorkspace = pWorkspaceFactory.Open(propSet, 0);
            return pWorkspace;
        }

        /// <summary>
        /// 获取SDE工作空间，通过Oracle直连方式 10G
        /// </summary>
        /// <param name="oraServer"></param>
        /// <param name="user"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static IWorkspace GetWorkspaceOracle10G(string oraServer, string user, string pwd)
        {
            IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass();
            IPropertySet propSet = new PropertySetClass();
            propSet.SetProperty("SERVER", "");
            propSet.SetProperty("INSTANCE", "sde:oracle10g");
            propSet.SetProperty("USER", user);
            propSet.SetProperty("PASSWORD", pwd + "@" + oraServer);
            propSet.SetProperty("VERSION", "SDE.DEFAULT");
            IWorkspace pWorkspace = pWorkspaceFactory.Open(propSet, 0);
            return pWorkspace;
        }

        /// <summary>
        /// 获取SDEFeatureClass
        /// </summary>
        /// <param name="strSDETableName">SDE表名称</param>
        /// <param name="pWorkspace">IWorkspace</param>
        /// <returns>IFeatureClass</returns>
        public static IFeatureClass GetSDEFeatureClass(string strSDETableName, IFeatureWorkspace pFeatureWorkspace)
        {
            if (string.IsNullOrEmpty(strSDETableName)) return null;
            if (pFeatureWorkspace == null) return null;
            IFeatureClass pSDEFeatureClass = pFeatureWorkspace.OpenFeatureClass(strSDETableName);
            return pSDEFeatureClass;
        }

        //public static IFeatureWorkspace GetSysFeatureWorkspaceFromSDE()
        //{
        //    //DataBaseLinkForm DataBaseLinkForm = new DataBaseLinkForm();
        //    string server = DataBaseLinkForm.ServerName;
        //    string user = DataBaseLinkForm.SDEUserName;
        //    string pass = DataBaseLinkForm.SDEUserPwd;
        //    return GetWorkSpaceFromSDE(server, user, pass, "SDE.DEFAULT", "5151") as IFeatureWorkspace;
        //}
    }
}
